package com.IO流.处理流.ObjectStream__对象流;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

public class ObjectInputStream__ {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        /***
         * 注意：
         * 1.序列化的类中建议添加    private static final long serialVersionUID = 1;  //类中添加和修改，不用重新序列化；提高兼容性‘
         * 2.序列化对象时默认所有属性进行序列化（除了static和transient修饰的成员），要确保对象的属性都实现
         * 3.序列化时怎么顺序保存的，反序列化时就按照之前的顺序恢复；否则出错
         * 例如存放顺序
         *         oos.writeInt(100);
         *         oos.writeBoolean(true);
         *         oos.writeDouble(3.14159265358979);
         *         oos.writeUTF("Java");
         *         oos.writeObject(new Dog("大黄",3));
         * 恢复顺序也要一致
         */
        String FilePath = "D:/JAVA_test/ObjectOutputStream.txt";
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FilePath));

        System.out.println(ois.readInt()); //恢复Int
        System.out.println(ois.readBoolean()); //boolean
        System.out.println(ois.readDouble());
        System.out.println(ois.readUTF());
        Object dog = ois.readObject();   //得到了存入的dog对象   这里会报异常，ClassNotFoundException 未找到类  抛出即可
        System.out.println(dog.getClass());  //得到dog的类型
        System.out.println(dog);    //输出dog
    }
}
